/**
 * CommandMenuController
 * 
 * Version 1.0
 *
 * Nov 29th, 2009
 * 
 * Copyright (c) 2009 Duc Nguyen
 */
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
/**
 * The command menu controllers listens to the edit menu commands lists and 
 * implements Edit Height, Edit Width, Edit Length, Redo, Undo. 
 * This uses JOptionPanes to get input values from the user.
 * 
 * @author Duc Nguyen
 *
 */

public class CommandMenuController extends BrickController {

	/**
	 * Default constructor
	 * @param frame		the brick frame this controller will refer to
	 */
	CommandMenuController(BrickFrame frame) {
		super(frame);
		
	}
	
	/**
	 * implementation of actionPerformed. 
	 * Will do something for redo, undo, edit height, width, length
	 */
	@Override
	public void actionPerformed(ActionEvent arg0) {
		String cmmd = arg0.getActionCommand();
		
		try{
			if(cmmd.equals("Redo")){
				this.getFrame().getCommandProcessor().redo();
	        }
			if(cmmd.equals("Undo")){
	        	this.getFrame().getCommandProcessor().undo();
	        }
		}catch(CommandException b){
			System.err.print(b.getMessage());
		}catch(CommandProcessorException c){
			JOptionPane.showMessageDialog(null, c.getMessage());
		}
		
		try{
			if(cmmd.equals("Edit Height")){
				editHeight();
			}
			if(cmmd.equals("Edit Width")){
				editWidth();
			}
			if(cmmd.equals("Edit Length")){
				editLength();
			}
		//case where user closes the dialog box
		}catch(NullPointerException n){
			/* Nothing Happens */
		//case where user enters a model specified invalid number
		}catch(CommandException b){
			JOptionPane.showMessageDialog(new JFrame(), 
					"There was an input Error. " + b.getMessage() 
					);
			//loop the actionPerformed; call the dialog box again
			this.actionPerformed(arg0);
		//case where the input is not a number
		}catch(NumberFormatException n){
			JOptionPane.showMessageDialog(new JFrame(), 
					"There was an input Error. Did you input something other than a number?" 
					);
			//loop the actionPerformed; call the dialog box again
			this.actionPerformed(arg0);
		}
	}
	
	/**
	 * opens a dialog box which gets the height input. If old input == new input, do nothing.
	 * @throws NullPointerException		if invalid input given in dialog box
	 * @throws CommandException		
	 */
	private void editHeight() throws NullPointerException, CommandException{
		String str;
		double oldHeight = this.getModel().getHeight();
		double newHeight;
		
		str = JOptionPane.showInputDialog(
				"Current height of Brick: " + this.getModel().getHeight() + 
				"\nPlease enter a new height"
				);

	
		newHeight = Double.parseDouble(str);

	
		//set height
		if(newHeight != oldHeight){
			this.getCommandProcessor().execute(new SetHeight(this.getModel(), newHeight));
		}
	
	}
	
	/**
	 * opens a dialog box which gets the width input. If old input == new input, do nothing
	 * @throws NullPointerException		if invalid input given in dialog box
	 * @throws CommandException
	 */
	private void editWidth() throws NullPointerException, CommandException{
		String str;
		double oldWidth = this.getModel().getWidth();
		double newWidth;
		
		str = JOptionPane.showInputDialog(
				"Current width of Brick: " + this.getModel().getWidth() + 
				"\nPlease enter a new width"
				);
		
		newWidth = Double.parseDouble(str);

		//set width if new width does not equal old width
		if(newWidth != oldWidth){
			this.getCommandProcessor().execute(new SetWidth(this.getModel(), newWidth));
		}
	}
	
	/**
	 * opens a dialog box which gets the length input. If old input == new input, do nothing
	 * @throws NullPointerException		if invalid input given in dialog box
	 * @throws CommandException
	 */
	private void editLength() throws NullPointerException, CommandException{
		String str;
		double oldLength = this.getModel().getLength();
		double newLength;
	
		
		str = JOptionPane.showInputDialog(
				"Current length of Brick: " + this.getModel().getLength() + 
				"\nPlease enter a new length"
				);

		newLength = Double.parseDouble(str);

		//set length
		if(newLength != oldLength){
			this.getCommandProcessor().execute(new SetLength(this.getModel(), newLength));
		}
	}
	
}
